package com.example.websocketserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Controller
public class MessageController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    // 处理广播消息
    @MessageMapping("/broadcast")
    @SendTo("/topic/public")
    public String handleBroadcast(String message) {
        return message;
    }

    // 处理点对点消息
    @MessageMapping("/private")
    public void handlePrivate(@Payload String message, SimpMessageHeaderAccessor headerAccessor) {
        String user = "";
        if (headerAccessor.getUser() != null) {
            user = headerAccessor.getUser().getName();
        }
        if (user != null) {
            simpMessagingTemplate.convertAndSendToUser(user, "/queue/private", message);
        }
    }

    // 接收客户端发送的消息
    @MessageMapping("/send")
    public void handleMessage(String message) {
        // 处理消息后广播到订阅地址
        messagingTemplate.convertAndSend("/topic/messages", message);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(new Date());
        System.out.println("收到客户端发来的消息" + formattedDate + ":" + message);
    }

    // 定时推送示例（需主类加@EnableScheduling）
    @Scheduled(fixedRate = 1000)
    public void sendPeriodicMessage() {
        String responseMessage1 = "{\n" +
                "  \"temperature\": {\n" +
                "    \"tool0\": {\n" +
                "      \"actual\": 214.8821,\n" +
                "      \"target\": 220.0,\n" +
                "      \"offset\": 0\n" +
                "    },\n" +
                "    \"tool1\": {\n" +
                "      \"actual\": 25.3,\n" +
                "      \"target\": null,\n" +
                "      \"offset\": 0\n" +
                "    },\n" +
                "    \"bed\": {\n" +
                "      \"actual\": 50.221,\n" +
                "      \"target\": 70.0,\n" +
                "      \"offset\": 5\n" +
                "    },\n" +
                "    \"history\": [\n" +
                "      {\n" +
                "        \"time\": 1395651928,\n" +
                "        \"tool0\": {\n" +
                "          \"actual\": 214.8821,\n" +
                "          \"target\": 220.0\n" +
                "        },\n" +
                "        \"tool1\": {\n" +
                "          \"actual\": 25.3,\n" +
                "          \"target\": null\n" +
                "        },\n" +
                "        \"bed\": {\n" +
                "          \"actual\": 50.221,\n" +
                "          \"target\": 70.0\n" +
                "        }\n" +
                "      },\n" +
                "      {\n" +
                "        \"time\": 1395651926,\n" +
                "        \"tool0\": {\n" +
                "          \"actual\": 212.32,\n" +
                "          \"target\": 220.0\n" +
                "        },\n" +
                "        \"tool1\": {\n" +
                "          \"actual\": 25.1,\n" +
                "          \"target\": null\n" +
                "        },\n" +
                "        \"bed\": {\n" +
                "          \"actual\": 49.1123,\n" +
                "          \"target\": 70.0\n" +
                "        }\n" +
                "      }\n" +
                "    ]\n" +
                "  },\n" +
                "  \"sd\": {\n" +
                "    \"ready\": true\n" +
                "  },\n" +
                "  \"state\": {\n" +
                "    \"text\": \"Operational\",\n" +
                "    \"flags\": {\n" +
                "      \"operational\": true,\n" +
                "      \"paused\": false,\n" +
                "      \"printing\": false,\n" +
                "      \"cancelling\": false,\n" +
                "      \"pausing\": false,\n" +
                "      \"sdReady\": true,\n" +
                "      \"error\": false,\n" +
                "      \"ready\": true,\n" +
                "      \"closedOrError\": false\n" +
                "    }\n" +
                "  }\n" +
                "}";
        String responseMessage2 ="{\n" +
                "  \"job\": {\n" +
                "    \"file\": {\n" +
                "      \"name\": \"whistle_v2.gcode\",\n" +
                "      \"origin\": \"local\",\n" +
                "      \"size\": 1468987,\n" +
                "      \"date\": 1378847754\n" +
                "    },\n" +
                "    \"estimatedPrintTime\": 8811,\n" +
                "    \"filament\": {\n" +
                "      \"tool0\": {\n" +
                "        \"length\": 810,\n" +
                "        \"volume\": 5.36\n" +
                "      }\n" +
                "    }\n" +
                "  },\n" +
                "  \"progress\": {\n" +
                "    \"completion\": 0.2298468264184775,\n" +
                "    \"filepos\": 337942,\n" +
                "    \"printTime\": 276,\n" +
                "    \"printTimeLeft\": 912\n" +
                "  },\n" +
                "  \"state\": \"Printing\"\n" +
                "}";
        messagingTemplate.convertAndSend("/topic/notifications", responseMessage1);
    }
}
